package com.launch.wlfw.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.launch.wlfw.common.Common;
import com.launch.wlfw.dao.ReaderInfoMapper;
import com.launch.wlfw.pojo.ReaderInfo;
import com.launch.wlfw.service.ReaderInfoService;
import com.launch.wlfw.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 读者信息服务impl
 *
 * @author liuwei
 * @date 2023/06/30
 */
@Slf4j
@Service("readerInfoService")
public class ReaderInfoServiceImpl implements ReaderInfoService {

    /**
     * 读者信息映射器
     */
    @Resource
    private ReaderInfoMapper readerInfoMapper;


    /**
     * 复述,模板
     */
    @Resource
    private RedisTemplate<String , Object> redisTemplate;


    @Resource
    private RedisService redisService;

    /**
     * 按名称查询用户信息和密码
     *
     * @param username 用户名
     * @param password 密码
     * @return {@link ReaderInfo}
     */
    @Override
    public ReaderInfo queryUserInfoByNameAndPassword(String username, String password) {
        return readerInfoMapper.queryUserInfoByNameAndPassword(username, password);
    }

    /**
     * 找到读者列表
     *
     * @param readerInfo 读者信息
     * @param pageNum    页面num
     * @param pageSize   页面大小
     * @return {@link PageInfo}<{@link ReaderInfo}>
     */
    @Override
    public PageInfo<ReaderInfo> findReaderList(ReaderInfo readerInfo,Integer pageNum,Integer pageSize) {
        Gson gson = new Gson();
        PageInfo<ReaderInfo> pageReaderInfoList;
        String key = Common.CACHE_READER_INFO_USER + Common.CACHE_SYMBOL_TWO
                + pageNum + Common.CACHE_SYMBOL_TWO
                + pageSize + Common.CACHE_SYMBOL_TWO
                + readerInfo.generateKey();

        if(Boolean.TRUE.equals(redisTemplate.hasKey(key))){
            String cachePageReaderInfo = String.valueOf(redisTemplate.opsForValue().get(key));
            pageReaderInfoList =  gson.fromJson(cachePageReaderInfo, new TypeToken<PageInfo<ReaderInfo>>() {
            }.getType());
            log.debug("从redis中查询阅读者信息列表,传入参数:{} , 结果:{}" , key , pageReaderInfoList);
            return pageReaderInfoList;
        }

        PageMethod.startPage(pageNum,pageSize);
        List<ReaderInfo> readerList = readerInfoMapper.findReaderList(readerInfo);
        pageReaderInfoList = PageInfo.of(readerList);
        redisTemplate.opsForValue().set(key , gson.toJson(pageReaderInfoList));
        log.debug("从Mysql中查询阅读者信息列表,传入参数:{} , 结果:{}" , key , pageReaderInfoList);
        return pageReaderInfoList;
    }

    /**
     * 找到读者通过id
     *
     * @param id id
     * @return {@link ReaderInfo}
     */
    @Override
    public ReaderInfo findReaderById(Integer id) {
        return readerInfoMapper.findReaderById(id);
    }

    /**
     * 插入阅读器列表
     * 插入
     *
     * @param readerInfo 读者信息
     * @return {@link Integer}
     */
    @Override
    public Integer insertReaderList(ReaderInfo readerInfo) {
        readerInfo.setRoleId(2);
        readerInfo.setPassword(SecureUtil.md5(Common.READER_INFO_PASSWORD));
        Integer integer = readerInfoMapper.insertReaderList(readerInfo);
        String keys = Common.CACHE_READER_INFO_USER + "*";
        redisService.sRemoveBy(keys);
        log.info("插入list中的用户数据成功,keys:{},传入参数:{}, 结果:{}"+ keys, keys, integer);
        return integer;
    }

    /**
     * 更新读者列表
     *
     * @param readerInfo 读者信息
     * @return {@link Integer}
     */
    @Override
    public Integer updateReaderList(ReaderInfo readerInfo) {
        Integer integer = readerInfoMapper.updateReaderList(readerInfo);
        String keys = Common.CACHE_READER_INFO_USER + "*";
        redisService.sRemoveBy(keys);
        log.info("更新list中的用户数据成功,keys:{},传入参数:{}, 结果:{}"+keys, keys, integer);
        return integer;
    }

    /**
     * 删除读者列表
     *
     * @param readerIds 列表
     * @return {@link Integer}
     */
    @Override
    public Integer deleteReaderList(List<String> readerIds) {
        Integer integer = readerInfoMapper.deleteReaderList(readerIds);

        //删除指定前缀的KEY
        //KEYS myPrefix:*

        String keys = Common.CACHE_READER_INFO_USER + "*";
        redisService.sRemoveBy(keys);
        log.info("删除list中的用户数据成功,keys:{},传入参数:{}, 结果:{}"+keys, keys, integer);
        return integer;
    }


    /**
     * 更新读者密码
     *
     * @param id     id
     * @param oldPwd 老pwd
     * @param newPwd 新pwd
     * @return {@link Integer}
     */
    @Override
    public Integer updateReaderPassword(Integer id, String oldPwd, String newPwd) {
        ReaderInfo readerInfo = readerInfoMapper.findReaderById(id);
        Integer resultCode = null;
        if (readerInfo.getPassword().equals(SecureUtil.md5(oldPwd))) {
            readerInfo.setPassword(SecureUtil.md5(newPwd));
            resultCode =  readerInfoMapper.updateReaderPassword(readerInfo);
        }
        return resultCode;
    }
}
